package com.jn.rxjava.presenter;

import android.content.Context;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.jn.rxjava.contract.FoodContract2;
import com.jn.rxjava.entity.FoodEntity;
import com.jn.rxjava.utils.LogUtils;

import java.io.IOException;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FoodPresenter2 implements FoodContract2.IFoodPresenter2 {

    private FoodContract2.IFoodView2 mMainView;
    private Context mContext;
    private OkHttpClient mClient;

    public FoodPresenter2(FoodContract2.IFoodView2 mMainView, Context mContext) {
        this.mMainView = mMainView;
        this.mContext = mContext;
        mClient = new OkHttpClient();
    }

    @Override
    public void getData(String keyWord) {//get方式异步请求
        Observable.create(new ObservableOnSubscribe<FoodEntity>() {
                    @Override
                    public void subscribe(@io.reactivex.rxjava3.annotations.NonNull ObservableEmitter<FoodEntity> emitter) throws Throwable {
                        Request request = new Request.Builder()
                                .url("https://shanhe.kim/api/za/calories.php?food=" + keyWord)
                                .build();
                        Call call = mClient.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                                LogUtils.d("asdf", "onFailure");
                                emitter.onError(e);
                            }

                            @Override
                            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                                String body = response.body().string();
                                String url = call.request().url().toString();
                                LogUtils.d("asdf", "url : " + url);
                                LogUtils.d("asdf", "onResponse : " + body);
                                Gson gson = new Gson();
                                FoodEntity foodEntity = gson.fromJson(body, FoodEntity.class);
                                emitter.onNext(foodEntity);
                            }
                        });
                    }
                }).observeOn(AndroidSchedulers.mainThread())// 指定 最后拿到数据操，解析，显示发生在主线程
                .subscribeOn(Schedulers.io())// 指定 网络请求耗时操作发生在子线程;
                /*.map(new Function<FoodEntity, List<FoodEntity.DataItem>>() {//对数据进行类型转换
                    @Override
                    public List<FoodEntity.DataItem> apply(FoodEntity foodEntity) throws Throwable {

                        return foodEntity.getData();
                    }
                }).subscribe(new Observer<List<FoodEntity.DataItem>>() {
                    @Override
                    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull List<FoodEntity.DataItem> foodEntity) {
                        if (mMainView != null) {
                            mMainView.showData(foodEntity);
                        }
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });*/
                .flatMap(new Function<FoodEntity, ObservableSource<FoodEntity.DataItem>>() {//将一个Observable转换成多个Observables，然后将发射的数据合并到一个单独的Observable，但数据是无序的
                    @Override
                    public ObservableSource<FoodEntity.DataItem> apply(FoodEntity foodEntity) throws Throwable {
                        return Observable.fromIterable(foodEntity.getData());
                    }
                }).subscribe(new Observer<FoodEntity.DataItem>() {
                    @Override
                    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull FoodEntity.DataItem s) {
                        LogUtils.d("asdf", "=========" + s.getFood());
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
